﻿namespace UsefulTools.ExtensionMethods
{
    using System.IO;
    using System.Linq;

    public static class Strings
    {
        /// <summary>
        /// Removes invalid path characters (returned by Path.GetInvalidPathChars method) from a given string by replacing them with underscore.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RemoveInvalidPathCharacters(this string str)
        {
            Throw.IfNull(str);
            return Path.GetInvalidPathChars().Aggregate(str, (current, ch) => current.Replace(ch, '_'));
        }

        /// <summary>
        /// Indicates whether the given string is empty or null.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool NullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        /// <summary>
        /// Returns the given string with quotes around it.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string InQuotes(this string str)
        {
            Throw.IfNull(str);
            return string.Format(@"""{0}""", str);
        }
        /// <summary>
        /// Formats the given string by inserting arguments' string representation.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Formatted(this string str, params object[] args)
        {
            Throw.IfNull(str);
            return string.Format(str, args);
        }
    }
}